home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9600 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Calling a function by reference?!
  5. Date: 11 Mar 1996 11:33:42 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4i1v6mINN5fk@keats.ugrad.cs.ubc.ca>
  8. References: <ga16wMlyZAQF088yn@ime.usp.br>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <ga16wMlyZAQF088yn@ime.usp.br>,
  12. Rogerio Brito <rbrito@ime.usp.br> wrote:
  13. >        Hello, All.
  14. >        
  15. >        I don't know if this question is so dumb, but this  is  a
  16. >        thing that is interesting from my point of view (that is,
  17. >        from  the point of view of a person that is not very much
  18. >        experienced):
  19. >        
  20. >        How  do I declare a function (say, foo) that receives two
  21. >        strings and does modifications to both?
  22. >
  23. >        Is there a better way than:
  24. >        
  25. >        void foo(char **argument1, char **argument2);
  26. >        
  27. >        ?? (It seems to be a dirty way... :( )
  28.  
  29. No. You may need to do this if the length of these strings can be changed by
  30. the function. If the function is length-preserving, you can just pass a pointer
  31. to the first character.
  32.  
  33. If you allow length changes, the function can only work with dynamic strings.
  34. You can't change the location of a character array, whether it is static or
  35. automatic. The routine will probably use malloc() and possibly realloc() to
  36. increase or decrease the size of the arguments.
  37.  
  38. Another way is to explicitly give the user a function which computes the size
  39. of the destination operand. For example, if you are concatenating the strings
  40. (which is already done by the standard library), you know that the size of the
  41. result is one less than the sum of the sizes of the two original strings (due
  42. to the deletion of one zero character). Since the user knows this, you can set
  43. up the function in such a way that the user provides enough space to store a
  44. result, and the two arguments are not modified. This gives the user freedom to
  45. use space obtained by calling malloc, or use static or automatic storage.
  46. -- 
  47.  
  48.